﻿#include <iostream>
#include <fstream>
#include <vector>
#include<string>
#include "SmartCar.h"
using namespace std;

// 输入小车信息
SmartCar inputSmartCarInfo(int carIndex) {
    SmartCar smartCar;

    // Car ID
    smartCar.carID = "cqusn20202148416666" + std::to_string(carIndex);

    // Chassis
    smartCar.chassis.number = "dp21020" + std::to_string(carIndex);
    smartCar.chassis.model = "SCOUT MINI";
    smartCar.chassis.wheelbase = "451mm";
    smartCar.chassis.track = "490mm";
    smartCar.chassis.groundClearance = "115mm";
    smartCar.chassis.minTurningRadius = "0m";
    smartCar.chassis.driveMode = "四轮四驱";
    smartCar.chassis.maxDistance = "10KM";
    smartCar.chassis.tires.push_back("型号：公路轮、麦克纳姆轮");
    smartCar.chassis.tires.push_back("尺寸：175mm");

    // AGX Kit
    smartCar.agxKit.model = "AGX Xavie+r";
    smartCar.agxKit.ai = "32 TOPS";
    smartCar.agxKit.cudaCore = "512";
    smartCar.agxKit.tensorCore = "64";
    smartCar.agxKit.memory = "32G";
    smartCar.agxKit.storage = "32G";

    // Camera
    smartCar.camera.model = "RealSense D435i";
    smartCar.camera.cameraType = "D430";
    smartCar.camera.resolution = "1920*1080";
    smartCar.camera.frameRate = "30";
    smartCar.camera.fov = "87*58";
    smartCar.camera.depthFrameRate = "90";

    // Lidar
    smartCar.lidar.model = "RS-Helios-16p";
    smartCar.lidar.channels = "16";
    smartCar.lidar.testRange = "100m";
    smartCar.lidar.powerConsumption = "8W";

    // Gyroscope
    smartCar.gyroscope.model = "CH110";
    smartCar.gyroscope.manufacturer = "NXP";

    // Display
    smartCar.display.size = "11.6";
    smartCar.display.model = "super";

    // Battery
    smartCar.battery.parameters = "24V/15Ah";
    smartCar.battery.externalPower = "24V";
    smartCar.battery.chargingTime = "2H";

    // Student
    cout << "输入第" << carIndex << "台小车的学号和姓名：" << endl;
    cin >> smartCar.student.studentID;
    cin >> smartCar.student.name;

    return smartCar;
}

// 将小车的信息存入文件
void saveSmartCarInfoToFile(const vector<SmartCar>& smartCars) {

    ofstream file("carInfo.txt");

    if (!file) {
        cerr << "无法创建文件。" << endl;
        return;
    }

    for (const SmartCar& smartCar : smartCars) {
        // Car ID
        file << "编号：" << smartCar.carID << "\n\n";

        // Chassis
        file << "底盘\n";
        file << "编号：" << smartCar.chassis.number << "\n";
        file << "型号：" << smartCar.chassis.model << "\n";
        file << "轴距：" << smartCar.chassis.wheelbase << "\n";
        file << "轮距：" << smartCar.chassis.track << "\n";
        file << "最小离地间隙：" << smartCar.chassis.groundClearance << "\n";
        file << "最小转弯半径：" << smartCar.chassis.minTurningRadius << "\n";
        file << "驱动形式：" << smartCar.chassis.driveMode << "\n";
        file << "最大行程：" << smartCar.chassis.maxDistance << "\n";
        file << "轮胎：\n";
        for (const string& tire : smartCar.chassis.tires) {
            file << tire << "\n";
        }
        file << "\n";

        // AGX Kit
        file << "AGX套件\n";
        file << "型号：" << smartCar.agxKit.model << "\n";
        file << "AI：" << smartCar.agxKit.ai << "\n";
        file << "CUDA核心：" << smartCar.agxKit.cudaCore << "\n";
        file << "Tensor CORE：" << smartCar.agxKit.tensorCore << "\n";
        file << "显存：" << smartCar.agxKit.memory << "\n";
        file << "存储：" << smartCar.agxKit.storage << "\n\n";

        // Camera
        file << "双目摄像头\n";
        file << "型号：" << smartCar.camera.model << "\n";
        file << "摄像头：" << smartCar.camera.cameraType << "\n";
        file << "RGB帧分辨率：" << smartCar.camera.resolution << "\n";
        file << "RGB帧率：" << smartCar.camera.frameRate << "\n";
        file << "FOV：" << smartCar.camera.fov << "\n";
        file << "深度帧率：" << smartCar.camera.depthFrameRate << "\n\n";

        // Lidar
        file << "多线激光雷达\n";
        file << "型号：" << smartCar.lidar.model << "\n";
        file << "通道数：" << smartCar.lidar.channels << "\n";
        file << "测试范围：" << smartCar.lidar.testRange << "\n";
        file << "功耗：" << smartCar.lidar.powerConsumption << "\n\n";

        // Gyroscope
        file << "9轴陀螺仪\n";
        file << "型号：" << smartCar.gyroscope.model << "\n";
        file << "厂家：" << smartCar.gyroscope.manufacturer << "\n\n";

        // Display
        file << "液晶显示屏\n";
        file << "尺寸：" << smartCar.display.size << "\n";
        file << "型号：" << smartCar.display.model << "\n\n";

        // Battery
        file << "电池模块\n";
        file << "参数：" << smartCar.battery.parameters << "\n";
        file << "对外供电：" << smartCar.battery.externalPower << "\n";
        file << "充电时长：" << smartCar.battery.chargingTime << "\n\n";

        // Student
        file << "学生信息\n";
        file << "学号：" << smartCar.student.studentID << "\n";
        file << "姓名：" << smartCar.student.name << "\n\n";

        file << "------------------------------\n\n";
    }

    file.close();
}

// 展示小车信息
void displaySmartCarInfo(const vector<SmartCar>& smartCars, int index) {
    const SmartCar& smartCar = smartCars[index];

    // Car ID
    cout << "编号：" << smartCar.carID << endl;

    // Chassis
    cout << "底盘" << endl;
    cout << "编号：" << smartCar.chassis.number << endl;
    cout << "型号：" << smartCar.chassis.model << endl;
    cout << "轴距：" << smartCar.chassis.wheelbase << endl;
    cout << "轮距：" << smartCar.chassis.track << endl;
    cout << "最小离地间隙：" << smartCar.chassis.groundClearance << endl;
    cout << "最小转弯半径：" << smartCar.chassis.minTurningRadius << endl;
    cout << "驱动形式：" << smartCar.chassis.driveMode << endl;
    cout << "最大行程：" << smartCar.chassis.maxDistance << endl;
    cout << "轮胎：" << endl;
    for (const string& tire : smartCar.chassis.tires) {
        cout << tire << endl;
    }
    cout << endl;

    // AGX Kit
    cout << "AGX套件" << endl;
    cout << "型号：" << smartCar.agxKit.model << endl;
    cout << "AI：" << smartCar.agxKit.ai << endl;
    cout << "CUDA核心：" << smartCar.agxKit.cudaCore << endl;
    cout << "Tensor CORE：" << smartCar.agxKit.tensorCore << endl;
    cout << "显存：" << smartCar.agxKit.memory << endl;
    cout << "存储：" << smartCar.agxKit.storage << endl;
    cout << endl;

    // Camera
    cout << "双目摄像头" << endl;
    cout << "型号：" << smartCar.camera.model << endl;
    cout << "摄像头：" << smartCar.camera.cameraType << endl;
    cout << "RGB帧分辨率：" << smartCar.camera.resolution << endl;
    cout << "RGB帧率：" << smartCar.camera.frameRate << endl;
    cout << "FOV：" << smartCar.camera.fov << endl;
    cout << "深度帧率：" << smartCar.camera.depthFrameRate << endl;
    cout << endl;

    // Lidar
    cout << "多线激光雷达" << endl;
    cout << "型号：" << smartCar.lidar.model << endl;
    cout << "通道数：" << smartCar.lidar.channels << endl;
    cout << "测试范围：" << smartCar.lidar.testRange << endl;
    cout << "功耗：" << smartCar.lidar.powerConsumption << endl;
    cout << endl;

    // Gyroscope
    cout << "9轴陀螺仪" << endl;
    cout << "型号：" << smartCar.gyroscope.model << endl;
    cout << "厂家：" << smartCar.gyroscope.manufacturer << endl;
    cout << endl;

    // Display
    cout << "液晶显示屏" << endl;
    cout << "尺寸：" << smartCar.display.size << endl;
    cout << "型号：" << smartCar.display.model << endl;
    cout << endl;

    // Battery
    cout << "电池模块" << endl;
    cout << "参数：" << smartCar.battery.parameters << endl;
    cout << "对外供电：" << smartCar.battery.externalPower << endl;
    cout << "充电时长：" << smartCar.battery.chargingTime << endl;
    cout << endl;

    // Student
    cout << "学生信息" << endl;
    cout << "学号：" << smartCar.student.studentID << endl;
    cout << "姓名：" << smartCar.student.name << endl;
    cout << endl;
}

int main() {
    vector<SmartCar> smartCars;
    // 输入十个小车的信息
    for (int i = 1; i <= 10; ++i) {
        SmartCar smartCar = inputSmartCarInfo(i);
        smartCars.push_back(smartCar);
    }

    // 存到文件里
    saveSmartCarInfoToFile(smartCars);

    // 展示文件中小车的信息
    ifstream file("carInfo.txt");

    if (!file) {
        cerr << "无法打开文件。" << endl;
        return 0;
    }

    string line;
    int carIndex = -1;

    while (true) {
        char choice;
        {
            do {
                cout << "按n显示下一辆小车信息，按p显示上一辆小车信息，按q退出：";
                cin >> choice;

                if (choice == 'n') {
                    ++carIndex;
                    if (carIndex <= 9) {

                        displaySmartCarInfo(smartCars, carIndex);
                    }
                    else {
                        cout << "已经是最后一辆小车了。" << endl;
                        carIndex = 9;
                    }

                }
                else if (choice == 'p') {
                    --carIndex;
                    if (carIndex >= 0) {
                        displaySmartCarInfo(smartCars, carIndex);
                    }
                    else {
                        cout << "已经是第一辆小车了。" << endl;
                        carIndex = 0;
                    }

                }

            } while (choice != 'n' && choice != 'p' && choice != 'q');

            if (choice == 'q') {
                break;
            }

        }
    }

    file.close();

    return 0;
}
